﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shsc.Net.Common.Validator.Attributes;
using Shsc.Net.Common.Util;
using Shsc.Net.Common.Resource;

namespace Shsc.Net.Common.Validator.Validators
{
    /// <summary>
    /// 验证动作基类
    /// </summary>
    public abstract class BaseValidator
    {
        public Shsc.Net.Common.Validator.Attributes.BaseValidationAttribute BaseValidationAttribute
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void DoValidate(string ctrlId,
            object value,
            string objectName,
            BaseValidationAttribute attribute,
            ValidatorResultList validationResults)
        {
            if (!DoValidate(value, attribute))
            {
                this.DoValidateMessage(ctrlId, value, attribute.Language, 
                    attribute.ErrorMessageId,                    
                    attribute.ErrorMessageResourceName, objectName,
                    attribute.ErrorMessageParam, validationResults);
            }
        }

        public abstract bool DoValidate(object value,
            BaseValidationAttribute attribute);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="errorMessageId"></param>
        /// <param name="errorMessageResourceName"></param>
        /// <param name="descName"></param>
        /// <param name="param"></param>
        /// <param name="validationResults"></param>
        private void DoValidateMessage(string ctrlId, object value, string language,
            string errorMessageId, string errorMessageResourceName,
            string objectName, string[] param,
            ValidatorResultList validationResults)
        {
            ValidatorResult result = new ValidatorResult();
            string content = "";

            content = MessageUtil.getMessage(errorMessageId, language,
                errorMessageResourceName, param);
            result.CtrlId = ctrlId;
            result.MessageContent = content;
            validationResults.Add(result);
        
        }
    }
}
